package com.lzp.appexp

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import java.lang.StringBuilder
import java.util.concurrent.Executors
import kotlin.system.measureTimeMillis

/**
 * @description:
 * @author: lxp
 * @date: 2022/4/14 上午10:41
 */
fun main() = runBlocking {
    /* flow {
         for (i in 0..5) {
             delay(1000)
             emit(i)
         }
     }.collect {
         println(it)
     }*/
    /*flowOf(1, 2, 3, 4, 5)
        .onEach {
            delay(1000)
        }
        .collect {
            println(it)
        }*/
    /*mutableListOf(1,2,3,4,5).asFlow()
        .onEach {
            delay(1000)
        }
        .collect {
            println(it)
        }*/

    /*val time = measureTimeMillis {
        flow<Int> {
            repeat(5){
                delay(100)
                emit(it)
            }
        }.collect {
            delay(100)
            println(it)
        }
    }
    println("$time")*/

    /*val time = measureTimeMillis {
        channelFlow<Int> {
            repeat(5){
                delay(100)
                send(it)
            }
        }.collect {
            delay(100)
            println(it)
        }
    }
    println("$time")*/
    /*val time = measureTimeMillis {
        flow<Int> {
            repeat(5) {
                delay(100)
                emit(it)
            }
        }.flowOn(Dispatchers.IO)
            .collect {
                delay(100)
                println(it)
            }
    }
    println("$time")*/

    /* flow {
         repeat(9){
             delay(100)
             emit(it)
         }
     }.map {
         it*it
     }.flowOn(Dispatchers.IO)
         .collect {
             println(it)
         }*/
    /*withTimeoutOrNull(2500){
        flow {
            repeat(5){
                delay(1000)
                emit(it)
            }
        }.collect {
            println(it)
        }
    }*/

    /* launch {
         for (k in 1..5) {
             delay(100)
             println("I'm blocked $k")
         }
     }

     sequence {
         for (i in 1..5) {
             Thread.sleep(100)
             yield(i)
         }
     }.forEach { println(it) }*/

    /*  launch {
          for (j in 1..5) {
              delay(100)
              println("I'm not blocked $j")
          }
      }

      flow {
          for (i in 1..5) {
              delay(100)
              emit(i)
          }
      }.onStart {
          println("开始")
      }.onCompletion {
          println("完成")
      }.collect {
          println(it)
      }*/
    /*val customerDispatcher = Executors.newFixedThreadPool(5).asCoroutineDispatcher()
    flow {
        repeat(5) {
            println("${Thread.currentThread().name}")
            delay(100)
            emit(it)
        }
    }.map {
        it * it
    }.flowOn(Dispatchers.IO).collect {
        println("${Thread.currentThread().name} = " + it)
    }

    println("done")*/

    /*repeat(5){
        repeat(it){
            print("*")
        }
        println()
    }

    println("*")
    println("**")
    println("***")
    println("****")*/

}